package com.yaoli.niuke;

import java.util.ArrayList;

/**
 * Created by siemens on 5/19/2017.
 */
public class _015_Test {
    ArrayList<Integer> list = new ArrayList<Integer>();

    public void Insert(Integer num) {
        if (list.size() == 0) {
            list.add(num);
        }else{
            boolean flag = false;
            for(int i = 0 ; i < list.size() ;i++){
                if(num < list.get(i)){
                    list.add(i,num);
                    flag = true;
                    break;
                }
            }
            if(flag == false){
                list.add(num);
            }
        }

    }

    public Double GetMedian() {
        //表明是偶数
        if(list.size() % 2 == 0 && list.size() >= 2){
            int a = list.size() / 2 - 1;
            int b = a + 1;
            return (((double)list.get(a) + (double)list.get(b)) / 2);
        }else{
            return ((double)list.get(list.size() / 2));
        }
    }

    public static void main(String[] args) {
        _015_Test test = new _015_Test();//5,2,3,4,1,6,7,0,8
        //5.00 3.50 3.00 3.50 3.00 3.50 4.00 3.50 4.00
        double a;
        test.Insert(5);
        a = test.GetMedian();
        System.out.println(a);
        test.Insert(2);
        a = test.GetMedian();
        System.out.println(a);
        test.Insert(3);
        a = test.GetMedian();
        System.out.println(a);
        test.Insert(4);
        a = test.GetMedian();
        System.out.println(a);
        test.Insert(1);
        a = test.GetMedian();
        System.out.println(a);
        test.Insert(6);
        a = test.GetMedian();
        System.out.println(a);
        test.Insert(7);
        a = test.GetMedian();
        System.out.println(a);
        test.Insert(0);
        a = test.GetMedian();
        System.out.println(a);
        test.Insert(8);
        a = test.GetMedian();
        System.out.println(a);
    }

}
